<%@page import="ContentManagers.RoleManagers.CustomerSessionManager"%>
<%@page import="ContentManagers.RoleManagers.ReviewManager"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" type="text/css" href="User Interface/css/user_int.css" />
    </head>
    <body>
         <% //employ temporary session management here
            try{
                boolean indb = false;

                indb = new CustomerSessionManager().checkSession(session.getAttribute("username").toString(), request.getRemoteAddr().toString(), session.getAttribute("sessionID").toString());

                if(indb == false){%>
                <%@ include file="index_header.jsp"%>
                <%  }
                else
                { %>
                <%@ include file="user_header.jsp" %>
                <% }
            //Call USER header file. The header contains the links for other pages. %> 
                
        <div id ="body_wrapper">
            <% //Actual Content of the USER Purchases page starts here. %>
      
            <%@ include file="default_left_sidebar.jsp" %>
            
            <% try{
                    ResultSet resultSet = new PMManager().getProduct(request.getParameter("productID").toString());
                    if(resultSet == null)
                        {
                            %> 
                        No items matched your search.
                            <%
                        }
                    
                    else
                        { %>
                        
                        <div id="view_item" class="content_box">
                            
                            <table>
                            <tr>
                                <td valign="top"> <div id="view_item_image"> 
                                    <img src="User Interface/images/default_item_bigimage.png">
                                </div> </td>
                                
                                <td> <div id="view_item_info">
                                    <%  
                                        while(resultSet.next())
                                        {   out.println("<productName>" + resultSet.getString("product_name") + "</productName>");
                                            out.println("<br><table><tr><td><smallerfont> <b> Synopsis: </b></td><td><smallerfont> " 
                                                        + resultSet.getString("description") + "</smallerfont></td></tr>");
                                            out.println("<tr><td><smallerfont><b> Price: </b></td><td> <smallerfont> Php. "
                                                        + resultSet.getString("price") + "</smallerfont></td></tr>");
                                        }
                                        %>
                                        </table>
                                        <smallerfont> ........................................................................................................................................
                                        </smallerfont>
                                        <br>
                                        <%  
                                            try{
                                                boolean hasReviews = false;
                                            
                                                hasReviews = new ReviewManager().checkIfReviewsExist(request.getParameter("productID").toString());

                                                if(hasReviews == false)
                                                { // item no reviews 

                                                }
                                                else{
                                                    //get and display comments
                                                    
                                                    ResultSet reviews = new ReviewManager().getProductReviews(request.getParameter("productID").toString());
                                                    
                                                    while(reviews.next())
                                                    {
                                                        out.println("<br> \" <comment> " + reviews.getString("review") + " </comment> \" <br> &nbsp;&nbsp;&nbsp <commentAuthor> - by " + reviews.getString("username") + "</commentAuthor><br>");
                                                    }//out.println("i'm here");
                                                    
                                                    } 
                                                }
                                            catch(Exception e){}
                                        
                                        
                                            try{
                                                boolean hasBought = false;
                                                hasBought = new PMManager().userHasBought(session.getAttribute("username").toString(), request.getParameter("productID").toString());
                                            
                                                if(hasBought == true)
                                                { %>
                                                    <br><br>Write review:<br>
                                                    <form name ="review_form" method="post" onsubmit="" action="ServletAddReview">
                                                        <textarea class="review_box" name="comment" rows="5" cols="10" maxlength="360" autocomplete="off"/></textarea>
                                                        <input type="text" name="product_id" class="invisible" value="<% out.println(request.getParameter("productID").toString()); %>"/>
                                                        <smallfont> Max of 360 characters. </smallfont>
                                                        <input type="submit" name="SubmitUserReview" value="Submit Review"/>
                                                    </form>
                                               <% }
                                               else{ %>
                                                      
                                                    <br><br>Write review:<br>
                                                    <form name ="review_form" method="" onsubmit="" action="">
                                                        <input type="text" class="review_box" value="You can only write reviews for the items you've purchased." name="comment" maxlength="360" autocomplete="off" disabled="disabled"/>
                                                        <Br><smallfont> Max of 360 characters. </smallfont>
                                                        <input type="submit" name="SubmitUserReview" value="Submit Review" disabled="disabled"/>
                                                    </form>
                                               
                                               <%}
                                            
                                            }catch(Exception e){}
                                        %>
                                        
                                        
                                        
                                        

                               </div> </td>
                            </tr>
                            </table>
                        
                        
                        </div>
                               
                        
                        
                      <%}
            
                    }catch(Exception e){ out.println("may nangyari"); }
            %>
            
            
                    
                    
            <% //Actual Content of the USER Purchases page ends here. %>
       </div>
        
       <%@ include file="user_footer.jsp" %>
       <% //Call USER footer file %>
    </body>
</html>


<% 
   }catch(Exception e)
        { response.sendRedirect("index.jsp");}%>